package com.ycsj.goldmedalnewconcept.teacher.activity;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.base.BaseApplication;
import com.ycsj.common.base.BusInfo;
import com.ycsj.common.manager.BqaManager;
import com.ycsj.common.manager.PopUpManager;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.common.HttpAddress;
import com.ycsj.goldmedalnewconcept.common.http.HttpCallBack;
import com.ycsj.goldmedalnewconcept.common.http.HttpUtils;
import com.ycsj.goldmedalnewconcept.teacher.adapter.SearchPopLvAdapter;
import com.ycsj.goldmedalnewconcept.teacher.adapter.SearchRvAdapter;
import com.ycsj.goldmedalnewconcept.teacher.bean.RvZsdInfo;
import com.ycsj.goldmedalnewconcept.teacher.bean.SearchInfo;
import com.ycsj.goldmedalnewconcept.teacher.bean.SearchItem;
import com.ycsj.goldmedalnewconcept.teacher.bean.ZsdInfo;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.autosize.internal.CancelAdapt;

/**
 * 按条件筛选
 */
public class SearchByConditionActivity extends BaseActivity implements CancelAdapt {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.bt_reset)
    Button btReset;
    @BindView(R.id.bt_ok)
    Button btOk;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.tv_search_by_zsd)
    TextView tvSearchByZsd;

    //按条件筛选列表数据
    private List<SearchInfo> datas;

    //按知识点筛选的弹框数据
    private List<RvZsdInfo> zsdDatas;

    //RecyclerView列表适配器
    private SearchRvAdapter adapter;

    //弹框知识点删选的请求服务器的数据
    //年级(1-11),单元(1-72),难度(1-3,多难度之间用_拼接),语言知识目标(多目标直接用_拼接),知识点(多知识点之间用_拼接)
    private String grade_D = "", unit_D = "", difficulty = "", knowledgeAim_D = "", knowledge = "";
    //年级的数据
    private List<SearchItem> gradeDatas;
    //知识点的数据
    private List<SearchItem> knowledgeAimDatas;
    //难度的数据
    private List<SearchItem> difficultyDatas;
    //所有单元的数据
    private List<SearchItem> unitDatas;

    //知识点刷新的标识
    public static final int UPDATEZSD = 0x660;


    @Override
    protected int getContentLayout() {
        return R.layout.activity_search_by_condition;
    }

    @Override
    protected void init() {
        //状态栏沉浸
        initStateBar();
        //初始化标题栏
        initTitle();
        //创建页面数据
        initData();
        //设置列表的数据
        initRv();
    }


    /**
     * 初始化标题栏
     */
    private void initTitle() {
        tvTitle.setText("按条件搜索");
    }

    /**
     * 创建页面是数据
     * 规则:1.去掉原来的级别,将小升初和中考放在年级里(年级里所有的标签都是单选)
     * 2.年级里的小升初和中考不能与单元同时选择,其它的标签可以和单元同时选择
     * 3.知识点筛选,做成2级
     */
    private void initData() {

        //初始化按条件筛选的数据
        datas = new ArrayList<>();
//        SearchInfo nj = new SearchInfo();
//        nj.searchName = "年级";
//        List<SearchItem> njItems = new ArrayList<>();
//        njItems.add(new SearchItem("一年级", false));
//        njItems.add(new SearchItem("二年级", false));
//        njItems.add(new SearchItem("三年级", false));
//        njItems.add(new SearchItem("四年级", false));
//        njItems.add(new SearchItem("五年级", false));
//        njItems.add(new SearchItem("六年级", false));
//        njItems.add(new SearchItem("七年级", false));
//        njItems.add(new SearchItem("八年级", false));
//        njItems.add(new SearchItem("九年级", false));
//        njItems.add(new SearchItem("小升初", false));
//        njItems.add(new SearchItem("中考", false));
//        nj.searachItems = njItems;
//        datas.add(nj);

//        SearchInfo yyzsmb = new SearchInfo();
//        yyzsmb.searchName = "语言\n知识\n目标";
//        List<SearchItem> yyItems = new ArrayList<>();
//        yyItems.add(new SearchItem("词汇", false));
//        yyItems.add(new SearchItem("句型结构", false));
//        yyItems.add(new SearchItem("语法", false));
//        yyItems.add(new SearchItem("阅读", false));
//        yyItems.add(new SearchItem("交际功能", false));
//        yyzsmb.searachItems = yyItems;
//        datas.add(yyzsmb);


//        SearchInfo nd = new SearchInfo();
//        nd.searchName = "难度";
//        List<SearchItem> ndItems = new ArrayList<>();
//        ndItems.add(new SearchItem("简单", false));
//        ndItems.add(new SearchItem("中等", false));
//        ndItems.add(new SearchItem("困难", false));
//        nd.searachItems = ndItems;
//        datas.add(nd);


        //1-1单元
        SearchInfo unit1 = new SearchInfo();
        unit1.searchName = "1-1";
        List<SearchItem> oneItems = new ArrayList<>();
        for (int i = 1; i < 19; i++) {
            oneItems.add(new SearchItem("Unit" + i, false));
        }
        unit1.searachItems = oneItems;
        datas.add(unit1);


        //1-2单元
        SearchInfo unit2 = new SearchInfo();
        unit2.searchName = "1-2";
        List<SearchItem> twoItems = new ArrayList<>();
        for (int i = 19; i < 37; i++) {
            twoItems.add(new SearchItem("Unit" + i, false));
        }
        unit2.searachItems = twoItems;
        datas.add(unit2);


        //1-3单元
        SearchInfo unit3 = new SearchInfo();
        unit3.searchName = "1-3";
        List<SearchItem> threeItems = new ArrayList<>();
        for (int i = 37; i < 55; i++) {
            threeItems.add(new SearchItem("Unit" + i, false));
        }
        unit3.searachItems = threeItems;
        datas.add(unit3);

        //1-3单元
        SearchInfo unit4 = new SearchInfo();
        unit4.searchName = "1-4";
        List<SearchItem> fourItems = new ArrayList<>();
        for (int i = 55; i < 73; i++) {
            fourItems.add(new SearchItem("Unit" + i, false));
        }
        unit4.searachItems = fourItems;
        datas.add(unit4);

        //年级的数据
        gradeDatas = datas.get(0).searachItems;

        //语言知识目标
        knowledgeAimDatas = datas.get(1).searachItems;

        //难度
        difficultyDatas = datas.get(2).searachItems;

        //单元
        unitDatas = new ArrayList<>();
        unitDatas.addAll(oneItems);
        unitDatas.addAll(twoItems);
        unitDatas.addAll(threeItems);
        unitDatas.addAll(fourItems);
    }

    /**
     * 设置列表的数据
     */
    private void initRv() {
        adapter = new SearchRvAdapter(this, R.layout.item_rv_search, datas, unitDatas);
        BqaManager.setRv(null, this, adapter, rv);
    }

    /**
     * 页面控件点击事件
     */
    @OnClick({R.id.iv_back, R.id.bt_ok, R.id.bt_reset, R.id.tv_search_by_zsd})
    public void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
            case R.id.bt_ok:
                //确定
                doCheckOkAction();
                break;
            case R.id.bt_reset:
                //重置,将页面的条件数据和知识点数据都设置为未选中状态
                resetDatas();
                break;
            case R.id.tv_search_by_zsd:
                //按知识点搜索
                shopSearhPop();
                break;
        }
    }


    /**
     * 点击确定后的操作
     */
    private void doCheckOkAction() {

        //拼接搜索条件
        appendSearchPoint();

        if (TextUtils.isEmpty(grade_D) && TextUtils.isEmpty(unit_D) && TextUtils.isEmpty(difficulty) && TextUtils.isEmpty(knowledgeAim_D) && TextUtils.isEmpty(knowledge)) {
            ToastUtils.showShort("您还未选择搜索条件");
        } else {
            //将所选择的条件传传递给TeacherAddQuestionsActivity界面
            BusInfo busInfo = new BusInfo();
            busInfo.type = TeacherAddQuestionsActivity.UPDATE;
            busInfo.difficulty = difficulty;
            busInfo.knowledge = knowledge;
            busInfo.knowledgeAim_D = knowledgeAim_D;
            busInfo.unit_D = unit_D;
            busInfo.grade_D = grade_D;
            EventBus.getDefault().post(busInfo);

            //关闭当前页面
            finish();
        }
    }

    /**
     * 将页面的所有数据都设置为未选中状态
     */
    private void resetDatas() {

        //重置所有搜索条件
        resetSearchPoint();

        //同时将知识点的数据集合清空
        if (null != zsdDatas && zsdDatas.size() != 0) {
            zsdDatas.clear();
        }

        //重置按条件筛选的所有数据状态
        for (int j = 0; j < datas.size(); j++) {
            List<SearchItem> searachItems = datas.get(j).searachItems;
            for (int k = 0; k < searachItems.size(); k++) {
                if (searachItems.get(k).isChoosed) {
                    searachItems.get(k).isChoosed = false;
                }
            }
        }

        adapter.notifyDataSetChanged();
    }

    /**
     * 弹出知识点搜索的弹框
     */
    private void shopSearhPop() {

        //重置搜索条件
        resetSearchPoint();

        //拼接搜索条件
        appendSearchPoint();

        //判断用户是否选择了搜索条件
        if (TextUtils.isEmpty(grade_D) && TextUtils.isEmpty(unit_D) && TextUtils.isEmpty(difficulty) && TextUtils.isEmpty(knowledgeAim_D) && TextUtils.isEmpty(knowledge)) {
            ToastUtils.showShort("您还未选择搜索条件");
        } else {
            //弹框
            PopUpManager.showPopByMatchParent(this, R.layout.item_pop_search, 0.3f, this, new PopUpManager.onGetViewListener() {
                @Override
                public void getChildView(View view, int layoutResId) {

                    //关闭按钮
                    ImageView lvClose = view.findViewById(R.id.lv_close);
                    lvClose.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //记录知识点选中的条目
                            for (int i = 0; i < zsdDatas.size(); i++) {
                                if (zsdDatas.get(i).isChoosed) {
                                    //先拼接knowledge_D和knowledge2_D,用-拼接
                                    String itemMsg = zsdDatas.get(i).name + "-" + zsdDatas.get(i).content;
                                    //每个选中的条目之间用_拼接
                                    knowledge += itemMsg + "_";
                                }
                            }

                            PopUpManager.dismiss();
                        }
                    });

                    //加载布局
                    final RelativeLayout rlLoadingView = view.findViewById(R.id.rl_loading);

                    //RecyclerView
                    final RecyclerView zsdrv = view.findViewById(R.id.rv);

                    Map<String, String> map = new HashMap<>();
                    map.put("type", "7");
                    map.put("grade_D", grade_D);
                    map.put("unit_D", unit_D);
                    map.put("difficulty", difficulty);
                    map.put("knowledgeAim_D", knowledgeAim_D);


                    HttpUtils.post(HttpAddress.ZSD, map, this, new HttpCallBack<ZsdInfo>() {
                        @Override
                        public void onSuccess(ZsdInfo item) {
                            rlLoadingView.setVisibility(View.GONE);
                            zsdrv.setVisibility(View.VISIBLE);

                            if ("200".equals(item.state)) {
                                //返回成功
                                zsdDatas = new ArrayList<>();
                                List<ZsdInfo.DataBean> data = item.data;
                                if (null != data) {
                                    for (int i = 0; i < data.size(); i++) {
                                        //获取知识点名称
                                        String name = data.get(i).knowledge_D;
                                        //获取知识点内容的集合
                                        List<String> knowledge2_d = data.get(i).knowledge2_D;
                                        for (int j = 0; j < knowledge2_d.size(); j++) {
                                            RvZsdInfo info = new RvZsdInfo();
                                            info.name = name;
                                            info.content = knowledge2_d.get(j);
                                            zsdDatas.add(info);
                                        }
                                    }
                                }

                                //设置列表数据
                                SearchPopLvAdapter popLvAdapter = new SearchPopLvAdapter(R.layout.item_pop_zsd, zsdDatas);
                                View emptyView = View.inflate(SearchByConditionActivity.this, R.layout.base_rv_common_empty_view_white_bg, null);
                                TextView tv = emptyView.findViewById(R.id.tv_content);
                                tv.setText("未查询到相关知识点");
                                BqaManager.setRv(emptyView, SearchByConditionActivity.this, popLvAdapter, zsdrv);
                            } else {
                                ToastUtils.showShort("异常,返回的状态码是:" + item.state);
                            }

                        }

                        @Override
                        public void onError(String e) {
                            rlLoadingView.setVisibility(View.GONE);
                            ToastUtils.showShort(BaseApplication.ERROR);
                        }
                    });
                }
            });
        }

    }

    /**
     * 拼接搜索条件
     */
    private void appendSearchPoint() {
        //赋值年级
//        for (int i = 0; i < gradeDatas.size(); i++) {
//            if (gradeDatas.get(i).isChoosed) {
//                grade_D = (i + 1) + "";
//            }
//        }
//        //赋值难度
//        for (int i = 0; i < difficultyDatas.size(); i++) {
//            if (difficultyDatas.get(i).isChoosed) {
//                difficulty += (i + 1) + "_";
//            }
//        }
//        if (!TextUtils.isEmpty(difficulty) && difficulty.length() > 0) {
//            difficulty = difficulty.substring(0, difficulty.length() - 1);
//        }

        //赋值单元
        for (int i = 0; i < unitDatas.size(); i++) {
            if (unitDatas.get(i).isChoosed) {
                unit_D = (i + 1) + "";
                break;
            }
        }

        //赋值语言知识目标
//        for (int i = 0; i < knowledgeAimDatas.size(); i++) {
//            if (knowledgeAimDatas.get(i).isChoosed) {
//                knowledgeAim_D += (i + 1) + "_";
//            }
//        }
//
//        if (!TextUtils.isEmpty(knowledgeAim_D) && knowledgeAim_D.length() > 0) {
//            knowledgeAim_D = knowledgeAim_D.substring(0, knowledgeAim_D.length() - 1);
//        }
//
//        //赋值知识点
//        if (null != zsdDatas && zsdDatas.size() != 0) {
//            for (int i = 0; i < zsdDatas.size(); i++) {
//                if (zsdDatas.get(i).isChoosed) {
//                    //先拼接knowledge_D和knowledge2_D,用-拼接
//                    String itemMsg = zsdDatas.get(i).name + "-" + zsdDatas.get(i).content;
//                    //每个选中的条目之间用_拼接
//                    knowledge += itemMsg + "_";
//                }
//            }
//
//            if (!TextUtils.isEmpty(knowledge) && knowledge.length() > 0) {
//                knowledge = knowledge.substring(0, knowledge.length() - 1);
//            }
//
//        } else {
//            knowledge = "";
//        }
    }

    /**
     * 重置搜索天骄
     */
    private void resetSearchPoint() {
        grade_D = "";
        difficulty = "";
        unit_D = "";
        knowledgeAim_D = "";
        knowledge = "";
    }

    /**
     * 事件处理
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        if (null != busInfo && busInfo.type == UPDATEZSD) {
            //更新搜索按钮的文本同时重置记录的知识点
            knowledge = "";
            //同时将知识点的数据集合清空
            if (null != zsdDatas && zsdDatas.size() != 0) {
                zsdDatas.clear();
            }
        }
    }
}
